<!--#include file="../Lib/base.asp"-->
<!--#include file="../Lib/Page.asp"-->
<%

	dim datadb,datadb2
	dim i
	dim act:act=lcase(Cls.fget("act",0))
	select case act
		case "adddb":adddb
		case "editdb":editdb
		case "del":del
		case "delsome":delsome
	end select
	
	sub adddb()
		dim keyword,types,content,PicMsgId,addtime,PicMsgListID
		
		keyword=Cls.enhtml(Cls.fpost("keyword",0))
		content=Cls.enhtml(Cls.fpost("content",0))
		PicMsgId=Cls.getint(Cls.fpost("PicMsgId",0),0)
		PicMsgListID=Cls.getint(Cls.fpost("PicMsgListID",0),0)
		types=Cls.enhtml(Cls.fpost("types",0))
		addtime=now()
		
		Dim data
		data=array(array("keyword",keyword,50,1),array("content",content,0,1),array("PicMsgId",PicMsgId,0,0),array("PicMsgListID",PicMsgListID,0,0),array("addtime",addtime,50,0),array("types",types,50,1))
		if Cls.db.dbnew("[sys_KeyWord]",data,"keyword='"&keyword&"'")=1 then
			Cls.echo "1"
		else
			Cls.echo "0该关键字已存在"
		end if	
		Cls.die
	end sub
	
	
	sub editdb()
		dim keyword,types,content,PicMsgId,addtime,PicMsgListID
		dim id:id=Cls.getint(Cls.fget("id",0),0)
		
		keyword=Cls.enhtml(Cls.fpost("keyword",0))
		content=Cls.enhtml(Cls.fpost("content",0))
		PicMsgId=Cls.getint(Cls.fpost("PicMsgId",0),0)
		PicMsgListID=Cls.getint(Cls.fpost("PicMsgListID",0),0)
		types=Cls.enhtml(Cls.fpost("types",0))
		
		Dim data
		data=array(array("keyword",keyword,50,1),array("content",content,0,1),array("PicMsgId",PicMsgID,0,0),array("PicMsgListID",PicMsgListID,0,0),array("types",types,50,1))
		if Cls.db.dbupdate("[sys_KeyWord]","id="&id,data)=1 then
			Cls.echo "1"
		else
			Cls.echo "0数据出错"
		end if	
		Cls.die	
	end sub
	
	
	sub del()
		dim id:id=Cls.getint(Cls.fget("id",0),0)
		if id>0 then
			Cls.db.dbdel "[sys_KeyWord]","id="&id&""
		end if
		Cls.echo "1"
		Cls.die
	end sub
	
	sub delsome()
		dim id:id=Cls.enhtml(Cls.fget("id",0))
		dim idarr:idarr=split(id,",")
		if ubound(idarr)<0 then
			Cls.echo "0至少选择一条信息"
		else
			dim i
			for i=0 to ubound(idarr)
				if not isnumeric(idarr(i)) then
					Cls.echo "0参数："&id(i)&"不正确，请确认后再操作"
					exit sub
				end if
			next
			Cls.db.dbdel "[sys_KeyWord]","id in("&id&")"
			Cls.echo "1"
		end if
		Cls.die
	end sub
	
%>
<!--#include file="../lib/Sys_Top.asp"-->
<script>
function checkadd(the)
{
	if($.trim(the.keyword.value)=="")
	{
		$.message({content:"关键字不能为空"});
		the.keyword.focus();
		return false;
	}
	if($('#types option:selected').val()=="文本")
	{
		if($.trim(the.Content.value)=="")
		{
			$.message({content:"回复内容不能为空"});
			the.Content.focus();
			return false;
		}
	}else if($('#types option:selected').val()=="图文"){		
		if($.trim(the.PicMsgId.value)=="")
		{
			$.message({content:"请选择图文素材"});
			return false;
		}
	}else{		
		if($.trim(the.PicMsgListID.value)=="")
		{
			$.message({content:"请选择多图文素材"});
			return false;
		}
	}
	var url,data;
	url="?act=adddb";
	data="keyword="+encodeURIComponent($.trim(the.keyword.value));
	data+="&Content="+encodeURIComponent($.trim(the.Content.value));
	data+="&PicMsgId="+encodeURIComponent($.trim(the.PicMsgId.value));
	data+="&types="+encodeURIComponent($.trim(the.types.value));
	data+="&PicMsgListID="+encodeURIComponent($.trim(the.PicMsgListID.value));
	
	$.ajax({
	type:"post",
	cache:false,
	url:url,
	data:data,
	error:function(){$.message({type:"error",content:"服务器错误，操作失败！"});},
	success:function(_)
	{
		var act=_.substring(0,1);
		var info=_.substring(1);
		switch(act)
		{
			case "0":
				$.message({type:"error",content:info});
				break;
			case "1":
				$.message({type:"ok",content:"保存成功"});
				setTimeout("location.href='?'",1000);
				break;
			default:
				alert(_)
				break;
		}
	}
	});
	return false

}

function checkedit(the,id)
{
	if($.trim(the.keyword.value)=="")
	{
		$.message({content:"关键字不能为空"});
		the.keyword.focus();
		return false;
	}
	if($('#types option:selected').val()=="文本")
	{
		if($.trim(the.Content.value)=="")
		{
			$.message({content:"回复内容不能为空"});
			the.Content.focus();
			return false;
		}
	}else if($('#types option:selected').val()=="图文"){		
		if($.trim(the.PicMsgId.value)=="")
		{
			$.message({content:"请选择图文素材"});
			return false;
		}
	}else{		
		if($.trim(the.PicMsgListID.value)=="")
		{
			$.message({content:"请选择多图文素材"});
			return false;
		}
	}
	var url,data;
	url="?act=editdb&id="+id+"";
	data="keyword="+encodeURIComponent($.trim(the.keyword.value));
	data+="&Content="+encodeURIComponent($.trim(the.Content.value));
	data+="&PicMsgId="+encodeURIComponent($.trim(the.PicMsgId.value));
	data+="&types="+encodeURIComponent($.trim(the.types.value));
	data+="&PicMsgListID="+encodeURIComponent($.trim(the.PicMsgListID.value));
	
	$.ajax({
	type:"post",
	cache:false,
	url:url,
	data:data,
	error:function(){$.message({type:"error",content:"服务器错误，操作失败！"});},
	success:function(_)
	{
		var act=_.substring(0,1);
		var info=_.substring(1);
		switch(act)
		{
			case "0":
				$.message({type:"error",content:info});
				break;
			case "1":
				$.message({type:"ok",content:"保存成功"});
				setTimeout("location.href='?'",1000);
				break;
			default:
				alert(_)
				break;
		}
	}
	});
	return false

}
$(function(){
	$(".del").click(function(){
		var id=this.getAttribute('rel');
		var throughBox=$.dialog.through;
			throughBox({
				icon:'question',
				content:'确定要删除？不可恢复！',
				lock:true,
				opacity:'0.5',
				ok:function(){
					var url='?act=del&id='+id;
					$.ajax({
					type:"get",
					cache:false,
					url:url,
					error:function(){$.message({type:"error",content:"服务器错误，操作失败！"});},
					success:function(_)
					{
						if(_.substring(0,1)==0)
						{
							$.message({type:"error",content:_.substring(1)});
						}
						else
						{
							$.message({type:"ok",content:"删除成功"});
							$("#list_"+id).fadeOut('slow');
						}
					}
					});
									
					},
				cancelVal:'取消',
				cancel:true 
			})
		
		})
	
	$(".delsome").click(function(){
		    var arrchk=$("input[name='id']:checked");
			var idarr="";
			$(arrchk).each(function(){
				if(idarr==""){idarr+=this.value}else{idarr+=","+this.value}                   
			}); 
			if(idarr=="")
			{
				$.message({content:"至少选择一条信息"});
			}
			else
			{
				var throughBox=$.dialog.through;
				throughBox({
				icon:'question',
				content:'确定要删除？不可恢复！',
				lock:true,
				opacity:'0.5',
				ok:function(){
					var url='?act=delsome&id='+idarr;
					$.ajax({
					type:"get",
					cache:false,
					url:url,
					error:function(){$.message({type:"error",content:"服务器错误，操作失败！"});},
					success:function(_)
					{
						var act=_.substring(0,1);
						var info=_.substring(1);
						switch(act)
						{
							case "0":
								$.message({type:"error",content:info});
								break;
							case "1":
								$.message({type:"ok",content:"删除成功"});
								var idnum;
								idnum=idarr.split(",")
								for(i=0;i<=idnum.length;i++)
								{
									$("#list_"+idnum[i]).fadeOut('slow');
								}
								break;
							default:
								alert(_);
								break;
						}
					}
					});
									
					},
				cancelVal:'取消',
				cancel:true 
			})
			}
		})
	
 })


function setclose(t0)
{
	if(t0=="文本"){
		$("#picmsg").css("display","none");
		$("#picmsglist").css("display","none");
		$("#textmsg").css("display","block");
	}else if(t0=="图文"){
		$("#picmsg").css("display","block");
		$("#picmsglist").css("display","none");
		$("#textmsg").css("display","none");
	}else{
		$("#picmsglist").css("display","block");
		$("#picmsg").css("display","none");
		$("#textmsg").css("display","none");
	}
}
function checksearch(a) {
    return "" == a.key.value || "请输入关键字" == a.key.value ? ($.message({
        content: "请输入关键字"
    }), a.key.focus(), !1) : void 0
}
</script>
</head>
<body>
<div id="notice"><span>当前位置：</span>栏目 > <a href="?">关键字设置</a></div>
	<%
	if act="edit" then	
	dim id:id=Cls.getint(Cls.fget("id",0),0)
	datadb=Cls.db.dbload("","keyword,types,content,PicMsgID,PicMsgListID","[sys_KeyWord]","id="&id,"")
	%>
    <script>	
	$(function(){
	<%if datadb(1,0)="文本" then%>
		$("#textmsg").css("display","");
		<%elseif datadb(1,0)="图文" then%>
		$("#picmsg").css("display","");
		<%else%>
		$("#picmsglist").css("display","");
		<%end if%>
	})
	</script>
<div class="clear_fixed">
  <dl class="addlist">
    <form onSubmit="return checkedit(this,<%=id%>)">
      <dt><em>关键字：</em>
        <input type="text" name="keyword" size="50" maxlength="50" value="<%=datadb(0,0)%>"/>
        设定关键字，当用户发送此关键字时将会自动回复消息</dt>
      <dt><em>回复类型：</em>
        <select name="types" id="types" onChange="setclose(this.value)">
          <option value="文本" <%if datadb(1,0)="文本" then Cls.echo "selected"%>>文本</option>
          <option value="图文" <%if datadb(1,0)="图文" then Cls.echo "selected"%>>图文</option>
          <option value="多图文" <%if datadb(1,0)="多图文" then Cls.echo "selected"%>>多图文</option>
        </select>
        【本文】为直接回复文本信息，【图文】为回复指定素材库里的主题</dt>
      <dt id="picmsg" style="display:none"><em>指定图文：</em>
        <select name="PicMsgId" id="PicMsgId">
          <option value="">请选择图片素材</option>
          <%datadb2=Cls.db.dbload("","id,title","[sys_PicMsg]","","id desc")
		  	if ubound(datadb2)>0 then
			for i=0 to ubound(datadb2,2)%>
          <option value="<%=datadb2(0,i)%>" <%if datadb(3,0)=datadb2(0,i) then Cls.echo "selected"%>><%=datadb2(1,i)%></option>
          <%next
		  end if%>
          </select>
      </dt>
      <dt id="picmsglist" style="display:none"><em>指定多图文素材：</em>
        <select name="PicMsgListID" id="PicMsgListID">
          <option value="">请选择多图文素材</option>
          <%datadb2=Cls.db.dbload("","p_id,p_title","[sys_PicMsgList]","","p_id desc")
		  	if ubound(datadb2)>0 then
			for i=0 to ubound(datadb2,2)%>
          <option value="<%=datadb2(0,i)%>" <%if datadb(4,0)=datadb2(0,i) then Cls.echo "selected"%>><%=datadb2(1,i)%></option>
          <%next
		  end if%>
          </select> 当自动回复时，会发送该多图文消息
      </dt>
      <dt id="textmsg" style="display:none"><em>回复内容：</em>
        <textarea name="Content" cols="60" rows="20"><%=datadb(2,0)%></textarea>
      </dt>
      <dd>
        <input type="submit" name="send" value="保存" />
        <input type="button" value="返回" onClick="location.href='javascript:history.go(-1)'" />
      </dd>
    </form>
  </dl>
</div>
<%elseif act="add" then%>
<%keyword=Cls.enhtml(Cls.fget("keyword",0))%>
<div class="clear_fixed">
  <dl class="addlist">
    <form onSubmit="return checkadd(this)">
      <dt><em>关键字：</em>
        <input type="text" name="keyword" size="50" maxlength="50" value="<%=keyword%>"/>
        设定关键字，当用户发送此关键字时将会自动回复消息</dt>
      <dt><em>回复类型：</em>
        <select name="types" id="types" onChange="setclose(this.value)">
          <option value="文本">文本</option>
          <option value="图文">图文</option>
          <option value="多图文">多图文</option>
        </select>
        【本文】为直接回复文本信息，【图文】为回复指定素材库里的主题</dt>
      <dt id="picmsg" style="display:none"><em>指定图文素材：</em>
        <select name="PicMsgId" id="PicMsgId">
          <option value="">请选择图文素材</option>
          <%datadb2=Cls.db.dbload("","id,title","[sys_PicMsg]","","id desc")
		  	if ubound(datadb2)>0 then
			for i=0 to ubound(datadb2,2)%>
          <option value="<%=datadb2(0,i)%>"><%=datadb2(1,i)%></option>
          <%next
		  end if%>
          </select> 当自动回复时，会发送该图文消息
      </dt>
      <dt id="picmsglist" style="display:none"><em>指定多图文素材：</em>
        <select name="PicMsgListID" id="PicMsgListID">
          <option value="">请选择多图文素材</option>
          <%datadb2=Cls.db.dbload("","p_id,p_title","[sys_PicMsgList]","","p_id desc")
		  	if ubound(datadb2)>0 then
			for i=0 to ubound(datadb2,2)%>
          <option value="<%=datadb2(0,i)%>"><%=datadb2(1,i)%></option>
          <%next
		  end if%>
          </select> 当自动回复时，会发送该多图文消息
      </dt>
      <dt id="textmsg"><em>回复文本内容：</em>
        <textarea name="Content" cols="60" rows="20"></textarea>
      </dt>
      <dd>
        <input type="submit" name="send" value="保存" />
        <input type="button" value="返回" onClick="location.href='javascript:history.go(-1)'" />
      </dd>
    </form>
  </dl>
</div>
<%else

dim key:key=Cls.enhtml(Cls.fget("key",0))
%>
<div class="clear_fixed">
  <div id="menu">
  <div class="search"><form action="?" method="get" onSubmit="return checksearch(this)"><input type="text" name="key" class="ip" value="<%if key<>"" then%><%=key%><%else%>请输入关键字<%end if%>" onFocus="if(this.value=='请输入关键字')this.value=''" onBlur="if(this.value=='')this.value='请输入关键字'" /><input type="submit" class="sbnt" value="查询" /></form></div>
    <dl>
      <dt><span><a href="?act=add">添加关键字</a></span></dt>
      <dt class="dropdown"><span><a href="javascript:;">批量操作↓</a></span>
        <ul>
          <li><a href="javascript:;" class="delsome">批量删除</a></li>
        </ul>
      </dt>
    </dl>
  </div>
  <form>
    <table cellpadding="5" id="table">
      <tr>
        <th width="20"><input type="checkbox" name="chkall" style="border:0;" onClick="checkall(this.form)" title="全选/取消" /></th>
        <th width="80">ID</th>
        <th width="100">回复类型</th>
        <th width="150">关键字</th>
        <th>回复内容/指定图文</th>
        <th width="100">调用次数</th>
        <th width="200">添加时间</th>
        <th width="80">管理</th>
      </tr>        
        <%
		dim sqlstr
		If key<>"" then
			sqlstr="where a.keyword like '%"&key&"%'"
		end if
		Set Page = new Page_List
		Page.Con = Cls.db.conn
		Page.Sql = "Select a.id as Kid,a.keyword,a.content,a.addtime,a.types,a.PicMsgID,a.PicMsgListID,a.hits,b.title,c.p_title from ([sys_KeyWord] a left join [sys_PicMsg] b ON a.PicMsgID=b.id) left join [sys_PicMsgList] c ON a.PicMsgListID=c.p_Id "&sqlstr&" order by a.id desc"
		Page.PageSize = 15
		Set Rs = Page.Rs
		
		If not Rs.Bof or Rs.Eof Then
			For i = 1 To Page.PageSize
				%>
                
      <tr id="list_<%=rs("Kid")%>">
        <td align="center"><input name="id" type="checkbox" style="border:0;" value="<%=rs("Kid")%>" /></td>
        <td align="center" ><%=rs("Kid")%></td>
        <td align="center"><%=rs("types")%></td>
        <td align="center"><%=rs("keyword")%></td>
        <td align="center" >【<%=rs("types")%>】<%if rs("types")="文本" then%><%=cls.cutstr(cls.nohtml(rs("content")),50,1)%><%elseif rs("types")="图文" then%><%if Cls.strlen(rs("title"))=0 then%><del style="color:#f00;">单图文数据被删除，请及时修正</del><%else%><%=rs("title")%><%end if%><%else%><%if Cls.strlen(rs("p_title"))=0 then%><del style="color:#f00;">多图文数据被删除，请及时修正</del><%else%><%=rs("p_title")%><%end if%><%end if%></td>
        <td align="center"><%=rs("hits")%></td>
        <td align="center" ><%=rs("addtime")%></td>
        <td align="center" ><a href="?act=edit&id=<%=rs("Kid")%>">编辑</a>　<a href="javascript:;" class="del" rel="<%=rs("Kid")%>">删除</a></td>
      </tr><%
				Rs.MoveNext
			Next
		End If
		
		Call Page.ShowPage
		
		Rs.Close
		Set Rs = Nothing
		Set Page = Nothing%>
    </table>
  </form>
</div>
<%end if%>
</body>
</html>
